/**
 * Created by Administrator on 2016/7/29.
 */
"use strict";
swan_pc
    .controller("MessagePersionalController",function($scope,messageManage,newMessageManage,TimeExchange, getAllUsers,
                                                      $q,messageManageById,ngDialog,GetAccountInfo,replyMessageManage,
                                                      updateMessageById){
        $scope.message=[];
        $scope.messageHasRead=[];
        $scope.messageNotRead=[];
        //来信列表获取初始化
        $scope.init=function(){
            $scope.messageNumber=0;
            $scope.meesageNotReadNumber=0;
            $scope.message=[];
            $scope.messageHasRead=[];
            $scope.messageNotRead=[];
            $scope.messageOrgNotRead=[];
            $scope.messageOrgHasRead=[];
            var def=$q.defer();
            var pro=def.promise;
            newMessageManage.query(
                function(result){
                    $scope.message=result;
                    angular.forEach($scope.message,function(data,index,array){
                        if(data.fromType="ORGANIZATION"){
                            if(data.isRead){
                                $scope.messageHasRead.push(data);
                            }else{
                                $scope.messageNotRead.push(data);
                            }
                        }else{
                            if(data.isRead){
                                $scope.messageOrgHasRead.push(data);
                            }else{
                                $scope.messageOrgNotRead.push(data);
                            }
                        }

                    })
                    def.resolve();
                }
            )
            pro.then(function(){
                $scope.messageNumber=$scope.messageNotRead.length+$scope.messageHasRead.length;
                $scope.messageNotReadNumber=$scope.messageNotRead.length;
                //$scope.messageOrgNumber=$scope.messageOrgNotRead.length+$scope.messageOrgHasRead.length;
                //$scope.messageOrgNotReadNumber=$scope.messageOrgNotRead.length;
                $scope.messageInView=$scope.message;
            })
        }
        $scope.init();
        //展示切换（全部未读已读）
        $scope.activeNumber=1;
        $scope.changeMessageView=function(option){
            if(option==1){
                $scope.activeNumber=1;
                $scope.messageInView=$scope.message;
            }else if(option==2){
                $scope.activeNumber=2;
                $scope.messageInView=$scope.messageHasRead;
            }else{
                $scope.activeNumber=3;
                $scope.messageInView=$scope.messageNotRead;
            }
        }
        //发信人姓名展示
        $scope.allUsers=[];
        getAllUsers.query(function(result){
            $scope.allUsers=result;
        })
        $scope.messageSenderNameShow=function(id){
            $scope.messageSenderName="";
            angular.forEach($scope.allUsers,function(data,index,array){
                if(data.id==id){
                    $scope.messageSenderName=data.firstName;
                }
            })
            return $scope.messageSenderName;
        }
        //好友申请处理
        $scope.AddFrinendMessageReply={
            reply:""
        };
        $scope.resolveString="ok";
        $scope.rejectString="cancel";
        $scope.resolveToAddFriend=function(id){
            $scope.AddFrinendMessageReply.reply="ok";
            replyMessageManage.save({id:id},$scope.AddFrinendMessageReply,function(result){
                console.log(result);
                $scope.init();
                if(result.type=="JOIN_INVITE"||result.type=="JOIN_APPLY_FROM_USER"){
                    swal("您已同意加入该组织","","success");
                }else {
                    swal("您已同意成为对方好友", "", "success");
                }
            },function(result){
                console.log(result);
                $scope.init();
                if(result.type=="JOIN_INVITE"||result.type=="JOIN_APPLY_FROM_USER"){
                    swal("同意加入该组织失败","","error")
                }else {
                    swal("同意建立好友关系失败", "", "error");
                }
            })

        }
        $scope.refuseToAddFriend=function(id){
            $scope.AddFrinendMessageReply.reply="cancel";
            replyMessageManage.save({id:id},$scope.AddFrinendMessageReply,function(result){
                console.log(result);
                $scope.init();
                swal("您已拒绝成为对方好友","","success");
            },function(result){
                console.log(result);
                $scope.init();
                swal("拒绝建立好友关系失败","","error");
            })
        }
        //邮件转发
        $scope.transmitMessage=function(id){
            $scope.transmittingMessage={
                "content": null,
                "fromId": 0,
                "fromType": "INDIVIDUAL",
                "isRead": false,
                "isResolved": false,
                "reply": null,
                "title": null,
                "toId": 0,
                "toType": "INDIVIDUAL",
                "type": "MSG_NORMAL"
            }
            var def=$q.defer();
            var pro=def.promise;
            angular.forEach($scope.message,function(data,index,array){
                if(data.id==id){
                    $scope.transMessage=data;
                    $scope.fromUserName=$scope.messageSenderNameShow($scope.transMessage.fromId);
                    $scope.transmittingMessage.content="转自"+$scope.fromUserName+":"+$scope.transMessage.content;
                    $scope.transmittingMessage.title=$scope.transMessage.title;
                    GetAccountInfo.get(function(result){
                            $scope.presentAccountInfo=result;
                            $scope.transmittingMessage.fromId=$scope.presentAccountInfo.id;
                        },
                        function(result){
                        })
                    ngDialog.open({
                        templateUrl:"scripts/components/yue/message/dialog.html",
                        scope:$scope,
                        controller:["$scope",function($scope){
                            $scope.selectFriendId=-1;
                            $scope.friendConfirm=function(){
                                $scope.transmittingMessage.toId=$scope.selectFriendId;
                                //console.log($scope.transmittingMessage);
                                ngDialog.closeAll();
                                def.resolve();
                            }
                        }]
                    })
                }
            })
            pro.then(function(){
                messageManage.save($scope.transmittingMessage, function (result) {
                    swal("转发信息成功", "", "success");
                }, function (result) {
                    swal("转发信息失败", "", "error");
                })
            })
        }
        //message删除操作
        $scope.deleteMessageByIds=function(ids){
            if(ids.length==0){
                swal("请选中短信后再点击批量删除");
            }
            else {
                swal({
                    title: "确定要删除已选中的短信吗",
                    text: "删除后将无法恢复！！",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确认删除",
                    cancelButtonText: "取消删除",
                    closeOnConfirm: false,
                    closeOnCancel: false
                }, function (isConfirm) {
                    if (isConfirm) {
                        angular.forEach(ids, function (data, index, array) {
                            messageManageById.delete({id: data}, function (result) {
                                $scope.init();
                            }, function () {
                                swal("删除失败", "", "error");
                            })
                            if (index == ids.length - 1) {
                                $scope.init();
                                swal("删除成功", "", "success");
                            }
                        })
                    } else {
                        swal("删除取消", "", "error");
                    }
                });
            }
        }
        $scope.deleteMessageById=function(id){
            swal({
                title: "确定要删除该短信吗",
                text: "删除后将无法恢复！！",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认删除",
                cancelButtonText: "取消删除",
                closeOnConfirm: false,
                closeOnCancel: false
            }, function(isConfirm) {
                if (isConfirm) {
                    messageManageById.delete({id:id},function(result){
                        $scope.init();
                        swal("删除成功","","success");
                    },function(){
                        swal("删除失败","","error");
                    })
                }else
                {     swal("删除取消","","error");
                }
            });
        }
        //messageContent显示
        $scope.messageContentShowFlag=-1;
        $scope.messageContentShowChange=function(option){
            if($scope.messageContentShowFlag==option){
                $scope.messageContentShowFlag=-1;
            }else {
                $scope.messageContentShowFlag = option;
            }
        }
        $scope.messageContentShow=function(option){
            if(option==$scope.messageContentShowFlag){
                return true;
            }else{
                return false;
            }
        }
        //读邮件
        $scope.changeIsReadById=function(id){
            $scope.readingMessage={};
            angular.forEach($scope.message,function(data,index,array){
                if(data.id==id){
                    $scope.readingMessage=data;
                }
            })
            if($scope.readingMessage.isRead==false){
                $scope.readingMessage.isRead=true;
                updateMessageById.update({},$scope.readingMessage,function(result){
                    $scope.init();
                },function(result){
                    console.log(result);
                })
            }else{

            }
        }
        //checkbox实现相关
        $scope.col="time";
        $scope.desc=0;
        $scope.selected = [];
        var updateSelected = function (action, id) {
            if (action == 'add' && $scope.selected.indexOf(id) == -1) $scope.selected.push(id);
            if (action == 'remove' && $scope.selected.indexOf(id) != -1) $scope.selected.splice($scope.selected.indexOf(id), 1);
        }
        $scope.updateSelection = function ($event, id) {
            var checkbox = $event.target;
            var action = (checkbox.checked ? 'add' : 'remove');
            updateSelected(action, id);
        };
        $scope.selectAll = function ($event) {
            var checkbox = $event.target;
            var action = (checkbox.checked ? 'add' : 'remove');
            for (var i = 0; i < $scope.message.length; i++) {
                var message = $scope.message[i];
                updateSelected(action, message.id);
            }
        };
        $scope.getSelectedClass = function (entity) {
            return $scope.isSelected(message.id) ? 'selected' : '';
        };
        $scope.isSelected = function (id) {
            return $scope.selected.indexOf(id) >= 0;
        };
        $scope.isSelectedAll = function () {
            return $scope.selected.length === $scope.message.length;
        };
    })